/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.eclass.servlet.front;

import cn.eclass.bean.SchoolLinkBean;
import cn.eclass.bean.api.ICategoryBean;
import cn.eclass.bean.api.ISchoolLinkBean;
import cn.eclass.entity.CategoryEntity;
import cn.eclass.entity.SchoolLinkEntity;
import cn.eclass.servlet.ActionFace;
import cn.eclass.utils.BeanFactory;
import cn.eclass.utils.ConstantsConf;
import cn.eclass.utils.Helper;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletException;

/**
 *
 * @author fortruth
 */
public class Category extends ActionFace {

    ICategoryBean bean = BeanFactory.factory.getCatBean();
    ISchoolLinkBean schoolLinkBean = BeanFactory.factory.getSchoolLinkBean();

    @Override
    public void doIndex() {
        List<CategoryEntity> categorys;
        CategoryEntity theCatgory;

        int catId;
        if (null == request.getParameter("catId")) {
            catId = 0;
            theCatgory = bean.getRoot();
            categorys = theCatgory.getChildren();
        } else {
            catId = Integer.parseInt(request.getParameter("catId"));
            theCatgory = getSecond(bean.getById(catId));
            categorys = bean.getByIdInTree(theCatgory.getId()).getChildren();
        }
        showSchoolLinksByCatId(categorys, catId);

    }

    private void showSchoolLinksByCatId(List<CategoryEntity> categorys, int catId) {
        List<SchoolLinkEntity> schoolLinks;
        int pageId = Integer.valueOf(Helper.getDefault(request.getParameter("pageId"), "1"));
        schoolLinks = schoolLinkBean.getSchoolLinkByCat(catId, ConstantsConf.ITEMS_PER_PAGE, pageId);
        int schoolLinksCount = schoolLinkBean.getMaxItemByCat(catId);

        showSchoolLinks(categorys, schoolLinks, schoolLinksCount, pageId);
    }

    private void showSchoolLinks(List<CategoryEntity> categorys, List<SchoolLinkEntity> schoolLinks, int schoolLinksCount, int pageId) {
        try {
            request.setAttribute("schoolLinks", schoolLinks);
            request.setAttribute("categorys", categorys);
            request.setAttribute("schoolLinksCount", schoolLinksCount);
            request.setAttribute("pageCount", Math.ceil((double) schoolLinksCount / ConstantsConf.ITEMS_PER_PAGE));
            request.setAttribute("pageSize", ConstantsConf.ITEMS_PER_PAGE);
            request.setAttribute("pageNo", pageId);
            request.getRequestDispatcher(view).forward(request, response);
        } catch (ServletException ex) {
            logger.error(ex);
        } catch (IOException ex) {
            logger.error(ex);
        }
    }

    public void doGetschoollinks() {
        int pageId = Integer.valueOf(Helper.getDefault(request.getParameter("pageId"), "1"));
        String searchString = request.getParameter("search");
        List<SchoolLinkEntity> schoolLinks;
        if (null != searchString) {
            searchString = Helper.toUTF8(searchString);
            schoolLinks = schoolLinkBean.search(searchString, ConstantsConf.ITEMS_PER_PAGE, pageId);
        } else {
            int catId = Integer.valueOf(Helper.getDefault(request.getParameter("catId"), "0"));
            schoolLinks = schoolLinkBean.getSchoolLinkByCat(catId, ConstantsConf.ITEMS_PER_PAGE, pageId);
        }

        request.setAttribute("schoolLinks", schoolLinks);
        try {
            request.getRequestDispatcher("/schoollinks.jsp").forward(request, response);
        } catch (ServletException ex) {
            logger.error(ex);
        } catch (IOException ex) {
            logger.error(ex);
        }
    }

    public void doSuggest() {
        Set<String> result = new HashSet<String>();
        result.add("hello");
        result.add("world");
        out.print(gsonNull.toJson(result));
    }

    private CategoryEntity getSecond(CategoryEntity theCategory) {
        if (theCategory.getParentId() == 0) {
            return theCategory;
        } else {
            return getSecond(bean.getByIdInTree(theCategory.getParentId()));
        }
    }

    public void doSearch() {
        String search = Helper.toUTF8(request.getParameter("search")).toLowerCase();
        CategoryEntity category = bean.getByName(search);
        if (null != category) {
            List<CategoryEntity> categorys;
            CategoryEntity theCatgory = getSecond(category);
            categorys = bean.getByIdInTree(theCatgory.getId()).getChildren();
            showSchoolLinksByCatId(categorys, category.getId());
        } else {
            int pageId = Integer.valueOf(Helper.getDefault(request.getParameter("pageId"), "1"));
            ISchoolLinkBean schoolBean = beanFactory.getSchoolLinkBean();

            List<SchoolLinkEntity> schoolLinks = schoolBean.search(search, ConstantsConf.ITEMS_PER_PAGE, pageId);

            int schoolLinkCount = schoolBean.countSearch(search);
            showSchoolLinks(null, schoolLinks, schoolLinkCount, pageId);
        }
        logger.info(search);
    }
}
